import random
import random
import requests
from bs4 import  BeautifulSoup

url = 'https://movie.douban.com/top250'

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
}
req = requests.get(url,headers=header)

move_info  = []

if req.status_code==200:
    soup = BeautifulSoup(req.text,'lxml')
    items = soup.find_all('div',class_='item')
    for item in items :
        title = item.find('span',class_='title').get_text(strip=True)
        rating_num = item.find('span',class_="rating_num").get_text(strip=True)
        move_info.append({'title':title,'rating':rating_num})
else:
    print("请求失败")

# filename = "1.txt"
# with open(filename,"w",encoding="utf-8") as f:
#     for move in move_info:
#         title=move['title']
#         rating= move['rating']
#         f.write(f"电影：{title},评分：{rating}\n")
# print(f"数据成功存储到：{filename}")





